package com.link.base.tencent.corpwx.usertag.controller;

import com.link.base.tencent.corpwx.usertag.model.UserTagItem;
import com.link.base.tencent.corpwx.usertag.service.UserTagItemService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成员标签-成员 
 *
 * @author 欧阳振强
 * @date 2020-01-06 10:53:25.0
 */
@Controller
@RequestMapping("/link/corpwx/userTagItem")
public class UserTagItemController extends BasicController<UserTagItem> {

    @Resource
    private UserTagItemService userTagItemService;

    @Override
    public BasicService<UserTagItem> getBasicService() throws Exception {
        return userTagItemService;
    }

    /**
     * 同步标签成员到企业微信
     *
     * @author 欧阳振强
     * @date 2020/01/06
     */
    @PostMapping(value = "/syncTagUserToWx", name = "同步标签成员到企业微信")
    @ResponseBody
    public Map<String, Object> syncTagUserToWx(@JsonParam UserTagItem userTagItem, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            userTagItemService.syncTagUserToWx(userTagItem);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除标签成员
     *
     * @author 欧阳振强
     * @date 2020/01/06
     */
    @PostMapping(value = "/delTagUser", name = "删除标签成员")
    @ResponseBody
    public Map<String, Object> delTagUser(@JsonParam UserTagItem userTagItem, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            userTagItemService.delTagUser(userTagItem);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 选择标签成员
     *
     * @author ouyangzhenqiang
     * @date 2019/07/25 14:00
     */
    @PostMapping(value = "/queryUnSelectUserListPage", name = "选择标签成员")
    @ResponseBody
    public Map<String, Object> queryUnSelectUserListPage(@JsonParam UserTagItem userTagItem, @JsonParam QueryParams qps,
                                                         HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            UserTagItem t = BasicModel.transformClass(UserTagItem.class, qps);
            t.setHeadId(userTagItem.getHeadId());
            List<UserTagItem> list = userTagItemService.queryUnSelectUserListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<UserTagItem>());
            result.put("result", e.getMessage());
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
        }
        return result;
    }
}